package com.infobie.android.pocketdhamma.dhammapada;

import com.infobie.android.pocketdhamma.R;
import com.infobie.android.pocketdhamma.dhammapada.DB.DBHelper;
import com.viewpagerindicator.CirclePageIndicator;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.text.SpannedString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class VerseView extends FragmentActivity {
	DBHelper dbHelper = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.verse_view); // default
                
        try
        {
        	Intent intent = this.getIntent();
        	int chapterId = (int)intent.getLongExtra("ChapterId", 0);
            String chapterDescription = intent.getStringExtra("ChapterDescription");
        	int initialVerseId = (int)intent.getLongExtra("VerseId", 0);

            TextView txtChapter = (TextView)findViewById(R.id.txtTitle2);
            txtChapter.setText(chapterDescription);
        	
        	dbHelper = new DBHelper(this);
        	Cursor cursor2Verses = dbHelper.GetVersesByChapterId(chapterId);
        	startManagingCursor(cursor2Verses);
        	
        	MyAdapter mAdapter = new MyAdapter(getSupportFragmentManager(), cursor2Verses);

        	ViewPager mPager = (ViewPager)findViewById(R.id.pager);
            mPager.setAdapter(mAdapter);
            
                        
            cursor2Verses.moveToFirst();
            int firstVerseId = cursor2Verses.getInt(cursor2Verses.getColumnIndex(DBHelper.colVerseId));
            int initialPosition = initialVerseId - firstVerseId;
            mPager.setCurrentItem(initialPosition);
            
            //Bind the circle indicator to the adapter
            CirclePageIndicator circleIndicator = (CirclePageIndicator)findViewById(R.id.circleIndicator);
            circleIndicator.setViewPager(mPager);
            
        }
        catch(Exception ex)
        {
        	Alerts.Show(this, "Error", ex.getMessage());
        }
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		dbHelper.close();
	}
	
	///
	/// Custom Pager Adapter
	///
    public static class MyAdapter extends FragmentPagerAdapter {
    	int _VerseCount = -1;
        Cursor _Cursor2Verses;
        
    	public MyAdapter(FragmentManager fm, Cursor cursor2Verses) {
            super(fm);
            _Cursor2Verses = cursor2Verses;
        }

        @Override
        public int getCount() {
        	if (_VerseCount < 0)
        		_VerseCount = _Cursor2Verses.getCount();
        	return _VerseCount;
        }

        @Override
        public Fragment getItem(int position) {
        	_Cursor2Verses.moveToPosition(position);
        	int verseId = _Cursor2Verses.getInt(_Cursor2Verses.getColumnIndex(DBHelper.colVerseId));
        	String verseName = _Cursor2Verses.getString(_Cursor2Verses.getColumnIndex(DBHelper.colVerseName));
        	String verseText = _Cursor2Verses.getString(_Cursor2Verses.getColumnIndex(DBHelper.colVerseText));
            return VerseFragment.newInstance(verseId, verseName, verseText);
        }
    }

    ///
    /// Custom Fragment
    ///
    public static class VerseFragment extends Fragment {
        int _VerseId;
        String _VerseName;
        String _VerseText;

        /**
         * Create a new instance of CountingFragment, providing "verseId"
         * as an argument.
         */
        static VerseFragment newInstance(int verseId, String verseName, String verseText) {
        	VerseFragment f = new VerseFragment();

            // Supply verseId input as an argument.
            Bundle args = new Bundle();
            args.putInt("VerseId", verseId);
            args.putString("VerseName", verseName);
            args.putString("VerseText", verseText);
            f.setArguments(args);

            return f;
        }

        /**
         * When creating, retrieve this instance's number from its arguments.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            _VerseId = getArguments() != null ? getArguments().getInt("VerseId") : 0;
            _VerseName = getArguments() != null ? getArguments().getString("VerseName") : "";
            _VerseText = getArguments() != null ? getArguments().getString("VerseText") : "";
        }

        /**
         * The Fragment's UI is just a simple text view showing its
         * instance number.
         */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.verse_pager, container, false);
            
            String sVerseText = 
            	    "<b>Verse " + _VerseId + ". " + _VerseName +  "</b><br><br>";
            if (null != _VerseText && _VerseText.length() > 0)
            	sVerseText = sVerseText + _VerseText;
            else
            	sVerseText = sVerseText + Html.toHtml(new SpannedString(getResources().getString(R.string.blank)));

            TextView txtVerseText = (TextView)v.findViewById(R.id.txtVerseText);
            txtVerseText.setText(Html.fromHtml(sVerseText));
            return v;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
        }
    }
}